﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Runtime.InteropServices;

/// <summary>
/// SnbPacker 的摘要说明
/// </summary>
public class BambookCore
{
    public enum BambookResult
    {
        succeed         =   0,     // 操作成功
        fail            =   1001,  // 操作失败
        notImplemented  =   1002,  // 该功能还未实现
        disconnected    =   1003,  // 与设备的连接已断开
        parameterError  =   1004,  // 调用函数传入的参数错误
        timeout         =   1005,  // 操作或通讯超时
        invalidHandle   =   1006,  // 传入的句柄无效
        invalidFile     =   1007,  // 传入的文件不存在或格式无效
        invalidDir      =   1008,  // 传入的目录不存在
        busy            =   1010,  // 设备忙，另一个操作还未完成
        endOfFile       =   1011,  // 文件或操作已结束
        ioError         =   1012,  // 文件读写失败
        fileNotInPack   =   1013   // 指定的文件不在包里
    }

    public enum BambookTransState
    {
        transfering,
        done,
        error
    }

    public enum BambookConnState
    {
        connected = 0,
        disconnected,
        connecting,
        waitingForAuthorating
    }

    public enum BambookKey
    {
        BBKeyNum0 = 0, BBKeyNum1, BBKeyNum2, BBKeyNum3, BBKeyNum4,
        BBKeyNum5, BBKeyNum6, BBKeyNum7, BBKeyNum8, BBKeyNum9,
        BBKeyStar, BBKeyCross, BBKeyUp, BBKeyDown, BBKeyLeft, BBKeyRight, BBKeyPageUp, BBKeyPageDown,
        BBKeyOK, BBKeyESC, BBKeyBookshelf, BBKeyStore, BBKeyTTS, BBKeyMenu, BBKeyInteract
    };

    // 设备信息结构
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct DeviceInfo
    {

        public UInt32 cbSize;              // 结构大小
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public char[] sn;                  // 设备序列号
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public char[] firmwareVersion;     // 设备固件版本
        public UInt32 deviceVolume;        // 设备总存储空间，单位为KB
        public UInt32 spareVolume;         // 剩余存储空间，单位为KB
    };

    // 自有书信息结构
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public struct PrivBookInfo
    {
        public UInt32 cbSize;              // 结构大小
        [MarshalAs(UnmanagedType.ByValArray,SizeConst=20)]
        public char[] bookGuid;            // 书籍id(在设备里的snb文件名)
        [MarshalAs(UnmanagedType.ByValArray,SizeConst=40)]
        public char[] bookName;            // 书名
        [MarshalAs(UnmanagedType.ByValArray,SizeConst=80)]
        public char[] bookAuthor;          // 作者
        [MarshalAs(UnmanagedType.ByValArray,SizeConst=256)]
        public char[] bookAbstract;        // 摘要
    };

    [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl)]
    public delegate void TransCallback(BambookTransState status, UInt32 progress, IntPtr userData);



    [DllImport("BambookCore.dll")]
    private static extern BambookResult BambookGetSDKVersion(ref int version);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookConnect(string lpszIP, UInt32 timeOut, ref UInt32 pConn);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookDisconnect(UInt32 hConn);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookGetConnectStatus(UInt32 hConn, ref BambookConnState status);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookGetDeviceInfo(UInt32 hConn,ref DeviceInfo pInfo);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookGetFirstPrivBookInfo(UInt32 hConn, ref PrivBookInfo pInfo);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookGetNextPrivBookInfo(UInt32 hConn, ref PrivBookInfo pInfo);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookAddPrivBook(UInt32 hConn, string pszSnbFile,
                                        TransCallback pCallbackFunc, IntPtr userData);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookReplacePrivBook(UInt32 hConn, string pszSnbFile, string lpszBookID,
                                            TransCallback pCallbackFunc, IntPtr userData);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookDeletePrivBook(UInt32 hConn, string lpszBookID);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookFetchPrivBook(UInt32 hConn, string lpszBookID, string lpszFilePath,
                                          TransCallback pCallbackFunc, IntPtr userData);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookKeyPress(UInt32 hConn, BambookKey key);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookPackSnbFromDir(string snbName, string rootDir);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookUnpackFileFromSnb(string snbName, string relativePath, string outputName);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookVerifySnbFile(string snbName);
    [DllImport("BambookCore.dll")]
    public static extern BambookResult BambookGetErrorString(UInt32 nCode);

	private BambookCore()
	{
	}
    ~BambookCore()
    {
    }
}
